package com.qidian.server.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.qidian.common.Constants;
import com.qidian.server.MServer;
import com.qidian.server.Status;

public class AnswerServlet extends HttpServlet
{
	private static final long serialVersionUID = -3970153729676852541L;
//	private static final Logger logger = Logger.getLogger(AnswerServlet.class.getName()); 
	private static final MServer mserver = MServer.getInstance() ;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException 
    {
        Status status = Status.OK ;
        String actionType = request.getParameter(ServletConstants.arg_action_type) ;
        
        if (actionType == null)
            status = Status.INVALID_ARGUMENT ;
        {
            if ("getAnswer".equalsIgnoreCase(actionType))
                status = getAnswer(request,response) ;
            else if ("report".equalsIgnoreCase(actionType))
                status = report(request,response) ;
            else
                status = Status.INVALID_ARGUMENT ;
        }
        
        if (status != Status.OK)
        {
            response.setCharacterEncoding("utf8") ;
            response.getWriter().write(status.statusCode.toString()) ;
        }
    }

   /**
    * 报告错题
 * @throws IOException 
    * 
    * */
    private Status report(HttpServletRequest request,
            HttpServletResponse response) throws IOException
    {
        String strId = request.getParameter(ServletConstants.arg_msg_id) ;
//        String maskCode = request.getParameter(ServletConstants.arg_mask_code) ; 
        String account = request.getParameter(ServletConstants.arg_account) ;
        String strStatus = request.getParameter(ServletConstants.arg_status) ;
        strStatus = strStatus==null?"0":strStatus ;
        if (strId == null || account == null)
        	return Status.INVALID_ARGUMENT ;
        long id = Long.parseLong(strId);
        Status status = mserver.p2s_report(id, account, strStatus) ; 
        if (status == Status.OK)
			response.getWriter().write(status.toString()) ;
            
        return status;
    }

    /**
     * 获取答案
     * @throws IOException 
     * 
     * */
    private Status getAnswer(HttpServletRequest request,
            HttpServletResponse response) throws IOException
    {
        String strId = request.getParameter(ServletConstants.arg_msg_id) ;
        StringBuffer result = new StringBuffer(8) ;

        if(strId == null)
        	return Status.INVALID_ARGUMENT ;
        long id = Long.parseLong(strId) ;
        Status status = mserver.p2s_getAnswer(id, null, null, result) ;
        response.setCharacterEncoding(Constants.answer_charset) ;
        if (status != Status.OK)
        {
			response.getWriter().write(strId+"#"+status.toString()) ;
        }
        else
        {
        	response.getWriter().write(strId+"#"+result.toString()) ;
        }
        return Status.OK;
    }

}
